package com.yztc.orangeentertainment.ui.main.findfragment.findhttp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;
import com.yztc.orangeentertainment.bean.findbean.FindChangeBean;
import com.yztc.orangeentertainment.bean.findbean.FindHotBean;
import com.yztc.orangeentertainment.bean.findbean.FindMoreBean;
import com.yztc.orangeentertainment.config.FindUrlConfig;
import com.yztc.orangeentertainment.utils.Okhttputils;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by My on 2016/10/13.
 */
public class FindHttpHelper {
    private static volatile FindHttpHelper singleton;

    private FindHttpHelper() {
    }

    public static FindHttpHelper getInstance() {
        if (singleton == null) {
            synchronized (FindHttpHelper.class) {
                if (singleton == null) {
                    singleton = new FindHttpHelper();
                }
            }
        }
        return singleton;
    }

    @NonNull
    private Retrofit getRetrofit() {
        return new Retrofit
                .Builder()
                //配置基础的url
                .baseUrl(FindUrlConfig.FindPath.BASE_URL)
                //配置提交或者返回的参数的造型方式为gson
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls
                        ().create()))
                //返回值可以使用Obserable
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //使用https时需要配置
                .client(new OkHttpClient())
                .build();
    }

    private FindRetrofitService getRetrofitService() {
        return getRetrofit().create(FindRetrofitService.class);
    }

    public void getFindChangBean(String type, Map<String, String> params, Subscriber<FindChangeBean>
            subscriber) {
        getRetrofitService()
                .getFindChangeBean(type, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getFindHotBean(String type, Map<String, String> params, Subscriber<FindHotBean>
            subscriber) {
        getRetrofitService()
                .getFindHotBean(type, params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getFindMoreBean(final String url, final Context context, final
    Subscriber<FindMoreBean>
            subscriber) {
        Observable.create(new Observable.OnSubscribe<FindMoreBean>() {
            @Override
            public void call(final Subscriber<? super FindMoreBean> subscriber) {
                try {
                    Okhttputils.getRequestEnq(url, context, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {
                                String str = response.body().string();
                                String jsonString = str.substring(str.indexOf("(") + 1, str
                                        .lastIndexOf(")"));
                                FindMoreBean findMoreBean = JSONObject.parseObject(jsonString,
                                        FindMoreBean
                                                .class);
                                subscriber.onNext(findMoreBean);
                                subscriber.onCompleted();
                            } else {
                                Toast.makeText(context, "请检查网络网络环境", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
